package com.zhuiyun.project.api.venuemanagement.service.impl;

import com.zhuiyun.project.api.venuemanagement.entity.BanRecord;
import com.zhuiyun.project.api.venuemanagement.entity.VenueManagement;
import com.zhuiyun.project.api.venuemanagement.mapper.VenueBanMapper;
import com.zhuiyun.project.api.venuemanagement.mapper.VenueManagementMapper;
import com.zhuiyun.project.api.venuemanagement.model.SelectSubordinateModel;
import com.zhuiyun.project.api.venuemanagement.model.VenueSelectModel;
import com.zhuiyun.project.api.venuemanagement.service.VenueBanService;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.pageUtil.MybatisPageHelper;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import com.zhuiyun.project.utils.pageUtil.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName VenueBanServiceImpl
 * @Description TODO
 * @Author wuwan
 * @Date 2023/12/6 10:23
 **/
@Service
public class VenueBanServiceImpl implements VenueBanService {
    @Autowired
    VenueBanMapper venueBanMapper;

    @Autowired
    VenueManagementMapper venueManagementMapper;


    /*
    * @Author wuwan
    * @Description 查询所有的被封禁数据
    * @Date 2023/12/6 11:03
    * @Return
    */
    @Override
    public CommonResult selectVenueBan(PageRequest pageRequest, String userName, String phone) {
        MybatisPageHelper.startPage(pageRequest);
        List<VenueSelectModel> venueManagements = venueBanMapper.selectVenueBan(userName, phone);
        PageResult pageResult = MybatisPageHelper.getPageResult(venueManagements);
        return CommonResult.ok(pageResult);
    }
    //    /*
//    * @Author wuwan
//    * @Description 场馆解封状态
//    * @Date 2023/11/6 9:48
//    * @Return
//    */
    @Override
    @Transactional
    public CommonResult changeUserStatusBan(BanRecord banRecord) {
        SelectSubordinateModel selectsubordinate = venueManagementMapper.selectDirectorIdcard(banRecord.getId());
        List<SelectSubordinateModel> selectsubordinate1 = venueManagementMapper.selectsubordinate(selectsubordinate.getVenueUniqueIdentifier());
        venueBanMapper.removeByTime(banRecord);
        for (int i = 0; i < selectsubordinate1.size(); i++) {
            SelectSubordinateModel selectSubordinateModel = selectsubordinate1.get(i);
            banRecord.setId(selectSubordinateModel.getId());
            venueBanMapper.changeUserStatusBan(banRecord);
        }
        return CommonResult.ok();
    }
    /*
     * @Author wuwan
     * @Description 查询封禁历史记录
     * @Date 2023/12/6 14:18
     * @Return
     */
    @Override
    public CommonResult getVenueBanRecord(String venueUniqueIdentifier) {
        return CommonResult.ok(venueBanMapper.getVenueBanRecord(venueUniqueIdentifier));
    }
    /*
    * @Author wuwan
    * @Description 定时解除封禁
    * @Date 2023/12/6 20:18
    * @Return
    */
    @Override
    public void changeLiftBan() {
        List<BanRecord> banRecords = venueBanMapper.selectUnique();
        for (int i = 0; i < banRecords.size(); i++) {
            BanRecord banRecord = banRecords.get(i);
            venueBanMapper.changeLiftBan(banRecord);
        }

    }
    /*
    * @Author wuwan
    * @Description 定时解除授权
    * @Date 2023/12/6 20:18
    * @Return
    */
    @Override
    public void changeAuthorizationExpiredTime() {
        List<BanRecord> banRecords = venueBanMapper.selectAuthorization();
        for (int i = 0; i < banRecords.size(); i++) {
            BanRecord banRecord = banRecords.get(i);
            venueBanMapper.changeAuthorizationExpiredTime(banRecord);
        }
    }
}
